package com.lonwin.inspectionmanagement.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;

/**
 * Author: zhangjing
 * DATA:2018/11/21 下午5:37
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class ArrowDownUpView extends LinearLayout{
    private ImageView ivArrow;
    private TextView tvtitle;
    private View mParentview;
    private ObjectAnimator objectAnimator;
    private String text;
    private boolean isopen;

    private ExpandListener mExpandListener;

    public void setExpandListener(ExpandListener expandListener) {
        mExpandListener = expandListener;
    }
    public ArrowDownUpView(Context context) {
        this(context,null);
    }

    public ArrowDownUpView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ArrowDownUpView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs,defStyleAttr);
    }
    private void init(Context context, AttributeSet attrs, int defStyleAttr){
        setOrientation(VERTICAL);
        if (mParentview==null){
            mParentview=inflate(getContext(),R.layout.item_arrowdownorup,this);
        }
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.ArrowDownUpView);
        text=typedArray.getString(R.styleable.ArrowDownUpView_btntext);
        ivArrow=mParentview.findViewById(R.id.iv_arrow);
        tvtitle=mParentview.findViewById(R.id.tv_title);
//        addView(mParentview);
        if (!TextUtils.isEmpty(text))tvtitle.setText(text);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isopen){
                    rotate180Arrow();
                    if (mExpandListener!=null){
                        mExpandListener.close(v);
                    }
                }else {
                    rotateArrow();
                    if (mExpandListener!=null){
                        mExpandListener.expand(v);
                    }
                }
            }
        });
        typedArray.recycle();
    }


    public void rotateArrow(){
        isopen=true;
        if (objectAnimator!=null){
            objectAnimator.cancel();
            objectAnimator=null;
        }
        objectAnimator = ObjectAnimator.ofFloat(ivArrow,"rotation", 0f, 180f);
        objectAnimator.setDuration(500);
        objectAnimator.start();
    }

    public void rotate180Arrow(){
        isopen=false;
        if (objectAnimator!=null){
            objectAnimator.cancel();
            objectAnimator=null;
        }
        objectAnimator = ObjectAnimator.ofFloat(ivArrow,"rotation", 180f, 0f);
        objectAnimator.setDuration(500);
        objectAnimator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        if (objectAnimator!=null){
            objectAnimator.cancel();
            objectAnimator=null;
        }
        super.onDetachedFromWindow();
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
//        int sizeWidth=MeasureSpec.getSize(widthMeasureSpec);
//        int heightMode=MeasureSpec.getMode(widthMeasureSpec);
//        int sizeHeight=MeasureSpec.getSize(widthMeasureSpec);
//        setMeasuredDimension((widthMode == MeasureSpec.AT_MOST ? ArmsUtils.dip2px(getContext(),100) : sizeWidth), (heightMode == MeasureSpec.EXACTLY ? ArmsUtils.dip2px(getContext(),100) : sizeHeight));;
//    }

   public interface ExpandListener{
        void expand(View view);
        void close(View view);
    }

    public void updateTitle(String title){
        this.text=title;
        tvtitle.setText(title);
    }
}
